package com.aso114.project.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aso114.project.R;
import com.aso114.project.base.BasesAdapter;
import com.aso114.project.bean.HistoryBean;
import com.aso114.project.commonview.CircleProgressView;
import com.aso114.project.mvp.activity.fragment.PracticeHistoryOneFragment;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asus on 2018/11/12.
 */

public class HistoryAdapter extends BasesAdapter<HistoryAdapter.ViewHolder> {

    private Context context;
    List<HistoryBean.Data> mData = null;
    private OnItemClickListener mClickListener;
    private int showtype = 1;// 1未完成  2完成
    public HistoryAdapter(Context context,List<HistoryBean.Data> data,int showtype) {
        super(context);
        mData = data;
        this.showtype = showtype;
        this.context = context;
    }

    //重写onCreateViewHolder方法，返回一个自定义的ViewHolder
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = getInflater().inflate(R.layout.practice_history_item,parent, false);
        ViewHolder holder= new ViewHolder(view,mClickListener);
        return holder;
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mClickListener = listener;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {

        HistoryBean.Data hdata = mData.get(position);

        holder.historyItemTitle.setText(hdata.getPaperTitle());



        try{
            if(hdata.getUpdateDate().equals("0")){

                String datetime = formatData("yyyy-MM-dd", Long.valueOf(hdata.getCreateDate()));
                holder.historyItemTime.setText(datetime);
            }else{
                String datetime = formatData("yyyy-MM-dd", Long.valueOf(hdata.getUpdateDate()));
                if(IsToday(datetime)){

                    System.out.println("题库大小111===" );//getQuestionBaseList()
                    String datetime1 = formatData("HH:mm", Long.valueOf(hdata.getUpdateDate()));
                    holder.historyItemTime.setText(datetime1);
                }else{
//                    String datetime1 = formatData("yyyy-MM-dd", Long.valueOf(hdata.getUpdateDate()));
                    holder.historyItemTime.setText(datetime);
                }
            }

        }catch (ParseException p){
            p.printStackTrace();
        }





//        Date date=new Date("2008-04-14");
        holder.historyItemNum.setText("共"+hdata.getQuestionsCount()+"道");

//        holder.historyItemProgress.setDownText("已做");

        int time = Integer.valueOf(hdata.getExpireDate());


        holder.history_time.setText(String.format("%02d",time/60)+":"+String.format("%02d",time%60));

//        holder.historyItemProgress.setTopTextColor(context.getResources().getColor(R.color.aff6559));
//        holder.historyItemProgress.setPaintColor(context.getResources().getColor(R.color.aff6559));
        if(showtype==1){//未完成
            holder.history_time.setVisibility(View.GONE);
            if(hdata.getAnswerCount().equals("0")){
                holder.historyItemProgress.setCurrent(0, true);
                holder.historyItemProgress.setDownText("已做");
                holder.historyItemProgress.setDownTextColor(context.getResources().getColor(R.color.a999999));
                holder.historyItemProgress.setTopTextColor(context.getResources().getColor(R.color.a00BF62));
                holder.historyItemProgress.setPaintColor(context.getResources().getColor(R.color.a00BF62));
            }else{
                double count =Double.valueOf(hdata.getAnswerCount())/Double.valueOf(hdata.getQuestionsCount())*100;
                int t = (int)count;
                holder.historyItemProgress.setCurrent(t, true);
                holder.historyItemProgress.setDownText("已做");
                holder.historyItemProgress.setDownTextColor(context.getResources().getColor(R.color.a999999));
                holder.historyItemProgress.setTopTextColor(context.getResources().getColor(R.color.a00BF62));
                holder.historyItemProgress.setPaintColor(context.getResources().getColor(R.color.a00BF62));
            }

        }else{
            //已完成

            holder.history_time.setVisibility(View.VISIBLE);

            if(hdata.getExamPaperType().equals("3")||hdata.getExamPaperType().equals("4")){
                //用于显示 真题喝巩固 的 红色圈
                if(hdata.getAnswerCount().equals("0")){
                    double count =Double.valueOf(hdata.getPaperSorce());
                    int t = (int)count;
                    holder.historyItemProgress.setCurrent(t, false);
                    holder.historyItemProgress.setDownText("得分");
//                    holder.historyItemProgress.setTop();
                    holder.historyItemProgress.setDownTextColor(context.getResources().getColor(R.color.a999999));
                    holder.historyItemProgress.setTopTextColor(context.getResources().getColor(R.color.aff6559));
                    holder.historyItemProgress.setPaintColor(context.getResources().getColor(R.color.aff6559));
                }else{
                    double count =Double.valueOf(hdata.getPaperSorce());
                    int t = (int)count;

                    holder.historyItemProgress.setCurrent(t, false);
                    holder.historyItemProgress.setDownText("得分");
                    holder.historyItemProgress.setDownTextColor(context.getResources().getColor(R.color.a999999));
                    holder.historyItemProgress.setTopTextColor(context.getResources().getColor(R.color.aff6559));
                    holder.historyItemProgress.setPaintColor(context.getResources().getColor(R.color.aff6559));
                }
            }else{
                //显示正确率
                if(hdata.getAnswerCount().equals("0")){
                    holder.historyItemProgress.setCurrent(0, true);
                }else{
                    double count =Double.valueOf(hdata.getAnswerCount())/Double.valueOf(hdata.getQuestionsCount())*100;
                    int t = (int)count;
                    holder.historyItemProgress.setCurrent(t, true);

                    holder.historyItemProgress.setDownText("正确率");
                    holder.historyItemProgress.setDownTextColor(context.getResources().getColor(R.color.a999999));
                    holder.historyItemProgress.setTopTextColor(context.getResources().getColor(R.color.a00BF62));
                    holder.historyItemProgress.setPaintColor(context.getResources().getColor(R.color.a00BF62));
                }
            }

        }

    }


    public static String formatData(String dataFormat, long timeStamp) {
        if (timeStamp == 0) {
            return "";
        }
        timeStamp = timeStamp * 1000;
        String result = "";
        SimpleDateFormat format = new SimpleDateFormat(dataFormat);
        result = format.format(new Date(timeStamp));
        return result;
    }

    public static boolean IsToday(String day) throws ParseException {

        Calendar pre = Calendar.getInstance();
        Date predate = new Date(System.currentTimeMillis());
        pre.setTime(predate);
        Calendar cal = Calendar.getInstance();
        Date date = getDateFormat().parse(day);
        cal.setTime(date);
        if (cal.get(Calendar.YEAR) == (pre.get(Calendar.YEAR))) {
            int diffDay = cal.get(Calendar.DAY_OF_YEAR)
                    - pre.get(Calendar.DAY_OF_YEAR);

            if (diffDay == 0) {
                return true;
            }
        }
        return false;
    }
    private static ThreadLocal<SimpleDateFormat> DateLocal = new ThreadLocal<SimpleDateFormat>();
    public static SimpleDateFormat getDateFormat() {
        if (null == DateLocal.get()) {
            DateLocal.set(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA));
        }
        return DateLocal.get();
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setHistoryType(int historytype) {
        this.showtype=historytype;
    }

    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @BindView(R.id.history_item_title)
        TextView historyItemTitle;
        @BindView(R.id.history_item_time)
        TextView historyItemTime;
        @BindView(R.id.history_time)
        TextView history_time;
        @BindView(R.id.history_item_num)
        TextView historyItemNum;
        @BindView(R.id.history_item_ly)
        LinearLayout historyItemLy;
        @BindView(R.id.history_item_progress)
        CircleProgressView historyItemProgress;
        private OnItemClickListener mListener;// 声明自定义的接口
        public ViewHolder(View itemView,OnItemClickListener listener) {
            super(itemView);
            ButterKnife.bind(this, itemView);

            mListener = listener;
            itemView.setOnClickListener(this);


        }

        @Override
        public void onClick(View v) {
            mListener.onItemClick(v, getPosition());
        }
    }


    public interface OnItemClickListener {
        public void onItemClick(View view, int postion);
    }
}
